package edu.isi.pedwork.data;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelWriter {
	HSSFWorkbook workbook;
	HSSFSheet factorlist;
	FileOutputStream fos = null;
	File file = null;
	
	public ExcelWriter(File factor)
	{
		this.file = factor;
		workbook = new HSSFWorkbook();
		factorlist = workbook.createSheet("FactorAndResponse");
	}
	
	public int writeHeader()
	{
		int index=0;
		HSSFRow row = factorlist.createRow(index);
		HSSFCell factorcell = row.createCell(0);
		factorcell.setCellValue(new HSSFRichTextString("Factor"));
		HSSFCell responseCell = row.createCell(1);
		responseCell.setCellValue(new HSSFRichTextString("Response"));
		index++;
		return index;
	}
	
	public int writeList(String type, ArrayList list, int index)
	{
		int index1 = index;
		for(int i=0; i<list.size(); i++){
			HSSFRow row = factorlist.createRow(index1);
			HSSFCell cell = row.createCell(0);
			cell.setCellValue(new HSSFRichTextString(type));
			HSSFCell cell1 = row.createCell(1);
			//cell1.setCellValue(new HSSFRichTextString(list.get(i).toString()));
			cell1.setCellValue(Double.parseDouble((list.get(i).toString())));
			index1++;
		}
		return index1;
	}
	
	public void write()
	{
		try {
			fos = new FileOutputStream(file);
			workbook.write(fos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(fos != null){
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
				
		}
	}
}
